﻿using Abp.Extensions;
using Sunlit.Business.Common.Dto;
using Sunlit.Business.HomeSliders;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Sunlit.Business.Common
{
    public class CommonAppService : BusinessAppServiceBase, ICommonAppService
    {
        public GetEnumSelectItemOutput GetEnumSelectItem()
        {
            var sliderType = GetEnum<SliderType>();
            var saleType = GetEnum<SaleType>();
            return new GetEnumSelectItemOutput
            {
                SliderType = sliderType.SelectItems,
                SliderTypeDic = sliderType.SelectItemDic,
                SaleType = saleType.SelectItems,
                SaleTypeDic = saleType.SelectItemDic
            };
        }
        private GetEnumOutput GetEnum<T>()
        {
            GetEnumOutput output = new GetEnumOutput();
            Type type = typeof(T);
            foreach (FieldInfo field in type.GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                var curDesc = field.GetCustomAttributes(typeof(DescriptionAttribute), false)
                    .FirstOrDefault()
                    .As<DescriptionAttribute>();
                var label = curDesc?.Description;
                var value = (int)field.GetValue(null);
                output.SelectItems.Add(new LabelValueDto { Label = label, Value = value });
                output.SelectItemDic.Add(value, label);
            }
            return output;
        }
    }
}
